package com.yichi.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yichi.R;
import com.yichi.YiChiApplication;
import com.yichi.model.TakeBatteryModel;

import java.util.ArrayList;
import java.util.List;

public class TakeBatteryAdapter extends RecyclerView.Adapter<TakeBatteryAdapter.TakeBatteryHolder> {
    public List<TakeBatteryModel> takeBatteryModels;

    public List<TakeBatteryModel> getTakeBatteryModels() {
        return takeBatteryModels;
    }

    public void setTakeBatteryModels(List<TakeBatteryModel> takeBatteryModels) {
        this.takeBatteryModels = takeBatteryModels;
    }

    public TakeBatteryAdapter() {
        this.takeBatteryModels = new ArrayList<>();
    }

    public TakeBatteryAdapter(List<TakeBatteryModel> takeBatteryModels) {
        this.takeBatteryModels = takeBatteryModels;
    }

    // 追加单条数据
    public void addItem(TakeBatteryModel newItem) {
        takeBatteryModels.add(newItem);
        notifyItemInserted(takeBatteryModels.size() - 1);
    }

    // 更新对象内容
    public void updateItem(int position, String newText) {
        if (position >= 0 && position < takeBatteryModels.size()) {
            takeBatteryModels.get(position).setBatteryNumber(newText);
            takeBatteryModels.get(position).setEmpty(false);
            notifyItemChanged(position); // 通知RecyclerView更新指定位置的数据
        }
    }

    // 删除指定位置的Item
    public void removeItem(int position) {
        if (position >= 0 && position < takeBatteryModels.size()) {
            takeBatteryModels.remove(position); // 移除数据
            notifyItemRemoved(position); // 通知RecyclerView移除item
            notifyItemRangeChanged(position, takeBatteryModels.size() - position); // 更新位置偏移
        }
    }

    @NonNull
    @Override
    public TakeBatteryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_take_battery, parent, false);
        return new TakeBatteryHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TakeBatteryHolder holder, int position) {
        TakeBatteryModel model = takeBatteryModels.get(position);
        holder.batteryNumber.setText(model.getBatteryNumber());
        if (model.isTakeOut()) {
            holder.itemView.setBackgroundResource(R.drawable.take_battery_bg);
            holder.batteryNumber.setTextColor(YiChiApplication.application.getResources().getColor(R.color.take_battery_text_color));
        } else {
            holder.itemView.setBackgroundResource(R.drawable.save_battery_bg);
            holder.batteryNumber.setTextColor(YiChiApplication.application.getResources().getColor(R.color.save_battery_text_color));
        }
        if (model.isEmpty()) {
            holder.itemView.setAlpha(0.4f);
            holder.batteryNumber.setVisibility(View.VISIBLE);
            holder.logo.setVisibility(View.GONE);
        } else {
            holder.itemView.setAlpha(1f);
            holder.batteryNumber.setVisibility(View.VISIBLE);
            holder.logo.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return takeBatteryModels.size();
    }

    static class TakeBatteryHolder extends RecyclerView.ViewHolder {
        ImageView logo;
        TextView batteryNumber;

        TakeBatteryHolder(View itemView) {
            super(itemView);
            logo = itemView.findViewById(R.id.logo);
            batteryNumber = itemView.findViewById(R.id.battery_number);

        }
    }
}